﻿using e_commerce.BLL.Data;
using e_commerce.BLL.Models;
using e_commerce.BLL.Services;
using e_commerce.BLL.Services.Client;
using e_commerce.Foreground.Models;
using e_commerce.Models;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace e_commerce.Foreground.Controllers
{
    public class ClientCenterController : Controller
    {
        private GoodsManager _gManager;
        private OrderService _oServeice;
        private DeliveryManager _dManager;
        // GET: ClientCenter

        public ClientCenterController()
        {
            _gManager = new GoodsManager();
            _oServeice = new OrderService();
            _dManager = new DeliveryManager();
        }

        [HandleError(ExceptionType = typeof(Exception))]
        public ActionResult Home()
        {
            return View();
        }

        [HandleError(ExceptionType = typeof(Exception))]
        public ActionResult OrderList()
        {
            var client = Session["Client"] as ClientModel;
            try
            {
                List<Models.Order> list = _oServeice.GetClientAllOrderById(client.ClientId)
               .Select(o => new Models.Order()
               {
                   OrderId = o.OrderId,
                   GoodsName = o.GoodsName,
                   Price = o.Price,
                   Count = o.Count,
                   CreateTime = o.CreateTime.ToString(),
                   DeliverName = o.Consignee,
                   Deliver = o.DeliveryAddress,
                   ClientMark = o.OrderRemark,
                   State = _oServeice.GetOrderState((OrderState)o.State),
                   StateBar = o.State,
                   Icon = _gManager.GetGoodsById(o.GoodsId).Image.First().ImageSrc,
               }).ToList();
                return View(list);
            }
            catch(Exception e)
            {
                return RedirectToAction("Login", "Account", new { callback = "/ClientCenter/OrderList" });
            }
        }

        [HandleError(ExceptionType = typeof(Exception))]
        public ActionResult OrderDetail(string orderId)
        {
            var order = _oServeice.GetOrderById(orderId);
            Models.Order model = new Models.Order()
            {
                OrderId = order.OrderId,
                GoodsName = order.GoodsName,
                Price = order.Price,
                Count = order.Count,
                CreateTime = order.CreateTime.ToString(),
                DeliverName = order.Consignee,
                Deliver = order.DeliveryAddress,
                ClientMark = order.ClientRemark,
                PhoneNumber = order.PhoneNumber,
                ServerMark = order.OrderRemark,
                State = _oServeice.GetOrderState((OrderState)order.State),
                StateBar = order.State,
                Icon = _gManager.GetGoodsById(order.GoodsId).Image.First().ImageSrc,
            };
            return View(model);
        }

        [HandleError(ExceptionType = typeof(Exception))]
        public ActionResult DeliveryInformation()
        {
            var client = Session["Client"] as ClientModel;
            List<Models.DeliveryInfo> list = _dManager.GetDeliveryByClientId(client.ClientId)
                .Select(d=>new Models.DeliveryInfo()
                {
                    Id = d.Id,
                    Name = d.Consignee,
                    Address = d.DetailedAddress,
                    PhoneNumber = d.PhoneNumber,
                    IsDefault = d.IsDefault,
                }).ToList();
            return View(list);
        }
        public ActionResult Center()
        {
            var client = Session["Client"] as ClientModel;
            return View(client);
        }

        [HttpPost]
        public ActionResult GetOrdersByState(int state)
        {
            var client = Session["Client"] as ClientModel;
            List<Models.Order> orders = _oServeice.GetOrderByState(client.ClientId, state)
                .Select(o => new Models.Order()
                {
                    OrderId = o.OrderId,
                    GoodsName = o.GoodsName,
                    Price = o.Price,
                    Count = o.Count,
                    CreateTime = o.CreateTime.ToString(),
                    DeliverName = o.Consignee,
                    Deliver = o.DeliveryAddress,
                    ClientMark = o.OrderRemark,
                    State = _oServeice.GetOrderState((OrderState)o.State),
                    StateBar = o.State,
                    Icon = _gManager.GetGoodsById(o.GoodsId).Image.First().ImageSrc,
                }).ToList(); ;
            return Json (new { orders = orders });
        }

        [HttpPost]
        public JsonResult OrderConfirm(string orderId)
        {
            var res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                _oServeice.ConfirmOrderByOrderId(orderId);
                success = "订单已完成";
            }catch(Exception ex)
            {
                error = ex.Message;
            }
            res.Data = new { success, error };
            return res;
        }

        [HttpPost]
        public JsonResult AddNewDiliverInfo(Models.DeliveryInfo d)
        {
            JsonResult res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                var client = Session["Client"] as ClientModel;
                BLL.Data.DeliveryInfo deliver = new BLL.Data.DeliveryInfo()
                {
                    Consignee = d.Name,
                    DetailedAddress = d.Address,
                    PhoneNumber = d.PhoneNumber,
                    ClientId = client.ClientId,
                };
                _dManager.AddNewDeliveryInfo(deliver);
                success = "地址已添加成功";
            }catch(Exception ex)
            {
                error = ex.Message;
            }
            res.Data = new { success,error};
            return res;
        }

        [HttpPost]
        public JsonResult DeleteDeliverInfo(int id)
        {
            JsonResult res = new JsonResult();
            var error = "";
            var success = "";
            try
            {
                _dManager.DeleteDelivery(id);
                success = "删除成功";
            }
            catch(Exception ex)
            {
                error = ex.Message;
            }
            res.Data = new { success, error };
            return res;
        }

        
        [HttpPost]
        public JsonResult SetDefaultDeliverInfo(int deliverId)
        {
            var res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                var client = Session["Client"] as ClientModel;
                _dManager.SetDefaultDelivery(client.ClientId, deliverId);
                success = "设置成功";
            }
            catch(Exception e)
            {
                error = e.Message;
            }
            res.Data = new { success, error };
            return res;
        }
    }
}